#!/bin/sh

root=`pwd`
MF="Makefile.am"

#create Makefile.am in code folder

#create main Makefile.am
createMainMakefile()
{
    printf "./$MF .........................."
    echo "#Makefile.am" > $MF
    echo "SUBDIRS=res include build" >> $MF
    echo "EXTRA_DIST=" >> $MF
    echo "" >> $MF
    echo "[OK]"
}

createMFInConf()
{
    cd $1
    dirName=$2
    dir=$1
    ConfMF="$root/res/$MF"
    echo "${dirName}dir=/opt/lonely_rabbit/${dir}" >> $ConfMF
    printf "$2_DATA=" >> $ConfMF
    for i in `ls | grep -v "svn" | grep -v "Makefile" | grep -v "\.$" | grep -v "$2$"`
    do
	printf " \\" >> $ConfMF
       printf "\n" >> $ConfMF
       printf "$1/$i" >> $ConfMF
    done
    printf "\n" >> $ConfMF
    cd "$root/res"
}

#create all Makefile.am files in conf directory
createConfMakefile()
{
    printf "./res/$MF ....................."
    cd "$root/res"
    echo "#conf/Makefile.am" > $MF
    
    createMFInConf "./conf" "conf"
    createMFInConf "./ui" "ui"

#    echo "confdir=/opt/lonely_rabbit" >> $MF
#    printf "conf_DATA=" >> $MF
#    files=`find . | grep -v svn | grep -v "\.$" | grep -v "conf$" | grep -v "ui$" | grep -v "avatar$" | grep -v "core$" | grep -v "UIComponent$" | grep -v "Makefile"`
#    for i in $files
#    do
#	printf " \\" >> $MF
#	printf "\n" >> $MF
#	printf "$i" >> $MF
#    done
#    printf "\n" >> $MF
#    echo "EXTRA_DIST=\${conf_DATA}" >> $MF
    echo "[OK]"
}

#create include/Makefile.am
createIncludeMakefile()
{
    printf "./include/$MF .................."
    cd "$root/include"
    echo "#include/Makefile.am" > $MF
    echo "lonely_rabbitdir=" >> $MF
    printf "lonely_rabbit_HEADERS=" >> $MF
    for i in `find . | grep ".h$" | grep -v "#"`
    do
	printf " \\" >> $MF
	printf "\n" >> $MF
        printf "$i" >> $MF
    done
    printf "\n">> $MF
    echo "[OK]"
}

#create build/Makefile.am
createSrcMakefile()
{
    printf "./build/$MF ...................."
    cd "$root/build"
    echo "#build/Makefile.am" > $MF
    echo "STATIC= -Werror -ggdb -O3 -Wall -ansi" >> $MF
    echo "AUTOMAKE_OPTIONS=foreign" >> $MF
    echo "bin_PROGRAMS = lonely_rabbit" >> $MF
    printf "lonely_rabbit_SOURCES=" >> $MF
    for i in `find ../src | grep ".cpp$" | grep -v "#"`
    do
	printf " \\" >> $MF
        printf "\n" >> $MF
        printf "$i" >> $MF
    done
    printf "\n">> $MF
    includes="-I../include -I../include/UIComponent -I../include/model -I../include/controller -I../include/view"
    echo "INCLUDES=$includes \`pkg-config --cflags goocanvas gtk+-2.0\` \$(STATIC)" >> $MF
    echo "LIBS=\`pkg-config --libs goocanvas gthread-2.0 gtk+-2.0\`" >> $MF
    echo "lonely_rabbit_LDFLAGS=\`pkg-config --cflags goocanvas gtk+-2.0\` \$(STATIC)" >> $MF
    echo "[OK]"
}

#running from here !!!
echo "==== clean up all history file ===="
rm build/.deps/* -rf

echo "==== create all makefile.am ===="
createMainMakefile
createConfMakefile
createIncludeMakefile
createSrcMakefile
#touch README NEWS AUTHORS ChangeLog COPYING

cd $root
aclocal && libtoolize --force && autoheader && autoconf && automake --add-missing
./configure --prefix=/usr
exit
